package com.fyzk.raceview;

import android.content.Context;
import android.graphics.Color;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.TextView;

import com.fyzk.bean.Race;
import com.fyzk.constants.ConstantsDJ;
import com.tyjc.lottery.R;

import java.util.ArrayList;
import java.util.List;


public abstract class RaceZuCaiiView extends RaceBaseView {

	private Context context;

	protected List<String> lotteryIds = new ArrayList<String>();
	protected List<String> wcodes = new ArrayList<String>();
	protected List<List<BetNum>> betNums = new ArrayList<List<BetNum>>();

//	@Override
	public View getRaceView(Context context, String content, List<Race> races, String prizeNum) {
//		// TODO Auto-generated method stub
//		this.context = context;
//		View view = LayoutInflater.from(context).inflate(
//				R.layout.detail_szc_race, null);
//		LinearLayout prize_ll = (LinearLayout) view
//				.findViewById(R.id.detail_szc_prize_ll);
//		TextView prize_tv = (TextView) view
//				.findViewById(R.id.detail_szc_prize_tv);
//		TextView fangAn_tv = (TextView) view
//				.findViewById(R.id.detail_szc_fangan_tv);
//		LinearLayout parent_ll = (LinearLayout) view
//				.findViewById(R.id.detail_szc_ll);
//		prize_tv.setValue(prizeNum);
//
//		splitContent(content, prizeNum);
//		allNum = betNums.size();
//
//		for (int i = 0; i < betNums.size(); i++) {
//			if (i < 10) {
//				View itemView = addItemView(i);
//				if (itemView != null) {
//					parent_ll.addView(itemView);
//				}
//			} else if (i == num) {
//				TextView textView = new TextView(context);
////				textView.setValue(
////		                 Html.fromHtml("<a href=\"http://www.dajiang365.com\">查看更多，请到网站进行查看</a> "));
//				textView.setValue("查看更多，请联系客服");
//				textView.setMovementMethod(LinkMovementMethod.getInstance());
//				textView.setTextColor(Color.BLUE);
//				textView.setGravity(Gravity.CENTER);
//				textView.setBackgroundColor(Color.WHITE);
//				parent_ll.addView(textView);
//			}
//		}
//
//		if (prizeNum != null && prizeNum.endsWith("hm")) {
//			prize_ll.setVisibility(View.GONE);
//			fangAn_tv.setVisibility(View.GONE);
//		}
//
		return new TextView(context);
	}

	private View addItemView(int i) {
		View view = LayoutInflater.from(context).inflate(
				R.layout.detail_szc_race_item, null);
		TextView wcode_tv = (TextView) view
				.findViewById(R.id.detail_szc_wcode_tv);
		GridView gv = (GridView) view.findViewById(R.id.detail_szc_gv);
		TextView num_tv = (TextView) view.findViewById(R.id.detail_szc_num_tv);

		wcode_tv.setText(ConstantsDJ.wcode(wcodes.get(i)));

		// gv.setEnabled(false);
		// gv.setNumColumns(14);
		// gv.setAdapter(new GridViewAdapter(betNums.get(i)));

		gv.setVisibility(View.GONE);
		List<BetNum> betNs = betNums.get(i);
		String codes = "";
		boolean isDan = false;
		for (int j = 0; j < betNs.size(); j++) {
			BetNum betNum = betNs.get(j);
			String code = betNum.betNum;
			// int color;
			// SpannableString ss = new SpannableString(codeValue);
			// if(betNum.isPrizeNum){
			// color = Color.RED;
			// }else{
			// color = Color.BLACK;
			// }
			// ss.setSpan(new ForegroundColorSpan(color), 0,
			// codeValue.length(),Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
			if (betNum.isPrizeNum) {
				if (j != betNs.size() - 1 && !isDan) {
					codes += "<font color=\"#ff0000\">" + code + "," + "</font>";
				}else{
					codes += "<font color=\"#ff0000\">" + code + "</font>";
				}
			} else {
				if (code.equals("(")) {
					isDan = true;
				}
				if (code.equals(")")) {
					isDan = false;
				}
				if (j != betNs.size() - 1 && !isDan) {
					codes += "<font color=\"#000000\">" + code + "," + "</font>";
				}else{
					codes += "<font color=\"#000000\">" + code + "</font>";
				}
			}

		}
		if (codes.contains("(,")) {
			codes.replace("(,","( ");
		}
		if (codes.contains(",)")) {
			codes.replace(",)"," )");
		}

		String htmlStr = "<html><body>" + codes + "</body></html>";
		num_tv.setText(Html.fromHtml(htmlStr));

		return view;
	}

	/**
	 * 拆分 投注内容
	 * 
	 * @param content
	 */
	protected abstract void splitContent(String content, String prizeNum);

	class GridViewAdapter extends BaseAdapter {

		private List<BetNum> betNums;

		public GridViewAdapter(List<BetNum> betNums) {
			this.betNums = betNums;
		}

		@Override
		public int getCount() {
			if (betNums == null) {
				return 0;
			}
			return betNums.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub

			BetNum betNum = betNums.get(position);
			convertView = LayoutInflater.from(context).inflate(
					R.layout.detail_szc_gv_item, null);

			TextView tv = (TextView) convertView
					.findViewById(R.id.detail_szc_gv_tv);
			tv.setText(betNum.betNum + "");
			if (betNum.isPrizeNum) {
				if (betNum.type == BetNum.TYPE_BULE) {
					tv.setTextColor(Color.BLUE);
				} else {
					tv.setTextColor(Color.RED);
				}
			}

			return convertView;
		}

	}

}
